package com.zjm.controller;

import com.zjm.entity.Student;
import com.zjm.entity.Teacher;
import com.zjm.service.StudentService;
import com.zjm.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService ;
    

    @RequestMapping("/login.do")
    public ModelAndView login(Integer id,String password,String level,HttpServletRequest request){
        HttpSession session = request.getSession();
        System.out.println("登录信息："+id+"   "+password+"   "+level);
        ModelAndView mv = new ModelAndView();
        if(null!=id && password!=null && level!=null){
            if(level.equals("stu")){
                Student student = new Student();
                student = studentService.login(id,password);
                System.out.println(student);
                if(student==null)
                    mv.setViewName("ULogin");
                else{
                    session.setAttribute("id",student.getId());
                    session.setAttribute("password",student.getPassword());
                    session.setAttribute("name",student.getName());
                    session.setAttribute("academy",student.getAcademy());
                    session.setAttribute("major",student.getMajor());
                    session.setAttribute("clas",student.getClas());
                    session.setAttribute("student",student);
                    mv.setViewName("Student");
                }

                return mv;
            }else if (level.equals("tea")){
                Teacher teacher = new Teacher();
                teacher = teacherService.login(id,password);
                System.out.println(teacher);
                if(teacher==null)
                    mv.setViewName("ULogin");
                else{
                    session.setAttribute("id",teacher.getId());
                    session.setAttribute("password",teacher.getPassword());
                    session.setAttribute("name",teacher.getName());
                    session.setAttribute("academy",teacher.getAcademy());
                    session.setAttribute("major",teacher.getMajor());
                    session.setAttribute("job",teacher.getJob());
                    session.setAttribute("teacher",teacher);
                    mv.setViewName("Teacher");
                }
                return mv;
            }
        }
        mv.setViewName("ULogin");
        return mv;
    }
}
